package com.finanzas.socioServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONValue;

import com.finanzas.dao.SocioDao;
import com.finanzas.entity.Socio;

@WebServlet("/SocioServletController")
public class SocioServletController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static Long COMISION_FINANZAS = 11L;
	
    @EJB
    private SocioDao socioDao;
	
    public SocioServletController() {
        super();
    }

    protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		Long operacion = Long.parseLong(request.getParameter("operacion"));
		Long idProyecto = Long.parseLong(request.getParameter("proyecto"));
		Map<String, String> catalogo = null;
		List<Map<String,String>>  list = new LinkedList<Map<String,String>>();
	
		if (operacion == 1) {
			List<Socio> listSocios = socioDao.consultarSociosProyecto(idProyecto);
			if (listSocios != null && listSocios.size() > 0) {
                for(Socio s:listSocios){
                	String nombreCompleto = s.getApellidoPaterno()+" "+s.getApellidoMaterno()+" "+s.getNombreSocio();
                	catalogo = new HashMap<String, String>();
                	catalogo.put("desc",nombreCompleto.toUpperCase());
                	catalogo.put("valor", s.getIdSocio().toString());
                	list.add(catalogo);
                }
              
			}else{
				String nombreCompleto = "No Existen Socios";
            	catalogo = new HashMap<String, String>();
            	catalogo.put("desc",nombreCompleto.toUpperCase());
            	catalogo.put("valor","0");
            	list.add(catalogo);
			}
			String jSon = JSONValue.toJSONString(list);
			out.println(jSon);
		}else if(operacion == 2){
			List<Socio> listSocios = socioDao.consultarSociosComisionProyecto(COMISION_FINANZAS,idProyecto);
			if (listSocios != null && listSocios.size() > 0) {
                for(Socio s:listSocios){
                	String nombreCompleto = s.getApellidoPaterno()+" "+s.getApellidoMaterno()+" "+s.getNombreSocio();
                	catalogo = new HashMap<String, String>();
                	catalogo.put("desc",nombreCompleto.toUpperCase());
                	catalogo.put("valor", s.getIdSocio().toString());
                	list.add(catalogo);
                }
              
			}else{
				String nombreCompleto = "No Existen Socios";
            	catalogo = new HashMap<String, String>();
            	catalogo.put("desc",nombreCompleto.toUpperCase());
            	catalogo.put("valor","0");
            	list.add(catalogo);
			}
			String jSon = JSONValue.toJSONString(list);
			out.println(jSon);
			
		}
	}
    

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
}
